package com.example.dell.library.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.dell.library.R;
import com.example.dell.library.bean.StaticStuBBook;
import com.example.dell.library.bean.Student;
import com.google.gson.Gson;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;

public class LoginActivity extends Activity {
    @BindView(R.id.login_no)
    EditText ed_loginNo;
    @BindView(R.id.login_password)
    EditText ed_loginPassword;
    Button btn_login;

    private String Account;//从bmob里获取的账号
    private String Password;//从bmob里获取的密码

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initViews();
        ButterKnife.bind(this);
    }

    private void initViews() {
        btn_login = findViewById(R.id.login_btn);
        btn_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                BmobQuerryStudent();
            }
        });
    }

    /**
     * 查询学生数据
     */

    private void BmobQuerryStudent() {
        BmobQuery<Student> bmobQuery = new BmobQuery<Student>();
        bmobQuery.findObjects(new FindListener<Student>() {
            @Override
            public void done(List<Student> list, BmobException e) {
                if (e == null) {
                    Account = ed_loginNo.getText().toString();
                    Password = ed_loginPassword.getText().toString();
                    for (int i = 0; i < list.size(); i++) {
                        String no = list.get(i).getStuNo();
                        String password = list.get(i).getPassword();
                        if (Account.equals(no) && Password.equals(password)) {
                            Student student = list.get(i);
                            StaticStuBBook sbbk = new StaticStuBBook();
                            sbbk.bid = student.getObjectId();

                            Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                            intent.putExtra("STUDENT", new Gson().toJson(student));
                            startActivity(intent);
                            break;
                        } else if ("".equals(Account) || "".equals(Password)) {
                            Toast.makeText(LoginActivity.this, "请输入学号和密码", Toast.LENGTH_SHORT).show();
                        } else if (list.size() - 1 == i && !Account.equals(no) && !Password.equals(password)) {
                            Toast.makeText(LoginActivity.this, "你输入的学号或者密码不正确", Toast.LENGTH_SHORT).show();
                        }

                    }
                }
            }
        });
    }
}
